package org.jeecg.modules.basic.behavior.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.behavior.entity.LwgyBehavior;
import org.jeecg.modules.basic.behavior.service.ILwgyBehaviorService;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.mapper.LwgyBaseStudentMapper;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 行为管理
 * @Author: jeecg-boot
 * @Date:   2021-03-09
 * @Version: V1.0
 */
@Api(tags="行为管理")
@RestController
@RequestMapping("/behavior/lwgyBehavior")
@Slf4j
public class LwgyBehaviorController extends JeecgController<LwgyBehavior, ILwgyBehaviorService> {
	@Autowired
	private ILwgyBehaviorService lwgyBehaviorService;

	 @Autowired
	 private LwgyBaseStudentMapper lwgyBaseStudentMapper;

	 @Autowired
	 private LwgyBaseBedMapper lwgyBaseBedMapper;

	 @Autowired
	 private ILwgyBaseStudentService lwgyBaseStudentService;

	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	
	/**
	 * 分页列表查询
	 *
	 * @param lwgyBehavior
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	 /**
	  * 分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "行为管理-分页列表查询")
	 @ApiOperation(value="行为管理-分页列表查询", notes="行为管理-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(LwgyBehavior lwgyBehavior,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<LwgyBehavior> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBehavior, req.getParameterMap());
		 Page<LwgyBehavior> page = new Page<>(pageNo, pageSize);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		 //管理员不限制
		 //辅导员只看他自己学生申请
		 //学生只看他提交的
		 List<Map<String,String>> fdyList = null;
		 if(roles.contains("fdy")){
			 List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
			 if (oConvertUtils.listIsNotEmpty((classId))) {
				 queryWrapper.in("student_class", classId);
			 } else {
				 queryWrapper.eq("student_class", "null");
			 }
			 /*		queryWrapper.in("admin_id",sysUser.getWorkNo());*/
		 }else if(roles.contains("user_students")){
			 queryWrapper.eq("student_number",sysUser.getWorkNo());
		 }

		 IPage<LwgyBehavior> pageList =lwgyBehaviorService.page(page, queryWrapper);

		 return Result.OK(pageList);
	 }
	/*
	@AutoLog(value = "行为管理-分页列表查询")
	@ApiOperation(value="行为管理-分页列表查询", notes="行为管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyBehavior lwgyBehavior,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyBehavior> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBehavior, req.getParameterMap());
		Page<LwgyBehavior> page = new Page<LwgyBehavior>(pageNo, pageSize);
		IPage<LwgyBehavior> pageList = lwgyBehaviorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}*/
	
	/**
	 *   添加
	 *
	 * @param lwgyBehavior
	 * @return
	 *//*
	@AutoLog(value = "行为管理-添加")
	@ApiOperation(value="行为管理-添加", notes="行为管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyBehavior lwgyBehavior) {
		lwgyBehaviorService.save(lwgyBehavior);
		return Result.OK("添加成功！");
	}*/
	 /**
	  *   添加
	  *
	  * @param lwgyBehavior
	  * @return
	  */
	 @AutoLog(value = "行为管理-添加")
	 @ApiOperation(value="行为管理-添加", notes="行为管理-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody LwgyBehavior lwgyBehavior) {
		 if (lwgyBehavior.getStudentNumber() != null){
			 LwgyBaseStudent lwgyBaseStudent =lwgyBaseStudentMapper.getStudentByNumber(lwgyBehavior.getStudentNumber());
			 if(oConvertUtils.isNotEmpty(lwgyBaseStudent)){
				 lwgyBehavior.setStudentId(lwgyBaseStudent.getId());
				 lwgyBehavior.setStudentCampus(lwgyBaseStudent.getStudentCampus());
				 lwgyBehavior.setStudentCollege(lwgyBaseStudent.getStudentCollege());
				 lwgyBehavior.setStudentMajor(lwgyBaseStudent.getStudentMajor());
				 lwgyBehavior.setStudentYear(lwgyBaseStudent.getStudentYear());
				 lwgyBehavior.setStudentClass(lwgyBaseStudent.getStudentClass());
			 }
			 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.getBedByStuId(lwgyBaseStudent.getId());
			 if(oConvertUtils.isNotEmpty(lwgyBaseBed)){
				 lwgyBehavior.setBuildingId(lwgyBaseBed.getBuildingId());
				 lwgyBehavior.setUnitId(lwgyBaseBed.getUnitId());
				 lwgyBehavior.setLayerId(lwgyBaseBed.getLayerId());
				 lwgyBehavior.setRoomId(lwgyBaseBed.getRoomId());
			 }
		 }
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String username = sysUser.getUsername();
		 lwgyBehavior.setFromUser(username);
		 lwgyBehaviorService.save(lwgyBehavior);
		 return Result.OK("添加成功！");
	 }
	/**
	 *  编辑
	 *
	 * @param lwgyBehavior
	 * @return
	 */
	@AutoLog(value = "行为管理-编辑")
	@ApiOperation(value="行为管理-编辑", notes="行为管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyBehavior lwgyBehavior) {
		lwgyBehaviorService.updateById(lwgyBehavior);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "行为管理-通过id删除")
	@ApiOperation(value="行为管理-通过id删除", notes="行为管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyBehaviorService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "行为管理-批量删除")
	@ApiOperation(value="行为管理-批量删除", notes="行为管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyBehaviorService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "行为管理-通过id查询")
	@ApiOperation(value="行为管理-通过id查询", notes="行为管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyBehavior lwgyBehavior = lwgyBehaviorService.getById(id);
		if(lwgyBehavior==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyBehavior);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyBehavior
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyBehavior lwgyBehavior) {
        return super.exportXls(request, lwgyBehavior, LwgyBehavior.class, "行为管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyBehavior.class);
    }

	 /**
	  *  批量确认
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "批量确认行为")
	 @ApiOperation(value="批量确认行为", notes="批量确认行为")
	 @PutMapping(value = "/batchBehavior")
	 public Result<?> batchBehavior(@RequestBody List<LwgyBehavior> lwgyBehaviors) {
		lwgyBehaviorService.updateBatchById(lwgyBehaviors);
		 return Result.OK("批量确认成功!");
	 }
}
